<?php

/*
 * yangzhic
 * 2016年8月17日下午6:50
 * OrderController
 */
namespace YMApi\Controller;

use Think\Controller;
use Think\Model;

class OrderSendmsgController extends Controller {
	
	
	/**
	 * 确认收货后2小时发短信
	 */
	
	public function myorder_confirm_sendmsg(){
	    
	    $list=M("order_confirm_sendmsg")->where(array('is_send'=>0,'send_time'=>array('between',(strtotime("-7 days")- date('Z')).','.(strtotime("now")- date('Z')))))->select();
	    foreach($list as $key=>$value){
	        $code=md5($value['id'].'sdx@9999');
	        switch($_SERVER['SERVER_NAME']){
	            case 'api.xian3.com':
	                $thisurl='xian3.com';
	                break;
	            case 'api.test.shidexian.com':
	                $thisurl='test.shidexian.com';
	                break;
	            case 'api.shidexian.com':
	                $thisurl='w.shidexian.com';
	                break;
	            default:
	                $thisurl='w.shidexian.com';
	                break;
	        }
	        $url='https://'.$thisurl.'?index&sid='.$value['id'].'&code='.$code.'#!/reted?ordId='.$value['order_sn'];
	        $url=$this->xlUrlAPI($url);
	        if($url){
    	        $send_result = A("Smsapi")->send_message_api('comment_201702',$value['mobile_phone'],$value['consignee'],0,$value['order_sn'],0,$value['uid'],array('url'=>urlencode($url)));
    	        M("order_confirm_sendmsg")->where(array('id'=>$value['id']))->setField('is_send',1);
    	        echo $value['order_sn'].' has been sent<br>';
	        }
	    }
	     
	}
	
	//没有评价，3天内默认好评
	public function default_comment(){
	    $list=M("order_confirm_sendmsg s")->join("left join sdx_comment c on s.order_sn=c.order_sn")->where(array('is_send'=>1,'send_time'=>array('between',(strtotime("-30 days")- date('Z')+(2*60*60)).','.(strtotime("-3 days")- date('Z')+(2*60*60))),'c.comment_id'=>array("EXP",'is NULL')))->select();
	    foreach($list as $item){
	        
	        $orderinfo = M("order_info")->where(array("order_id"=>$item['order_id']))->find();
	        
	        if(empty($orderinfo)){
	            continue;
	        }
	        $order_goods = M("order_goods")->where(array("order_id"=>$orderinfo['order_id']))->select();
	        
	        $goods_le_arr=array();
	        foreach($order_goods as $goods_item){
	            $goods_le_arr[]=$goods_item['goods_id'].",5";
	        }
	        
	        
	        
	        $goods_level=implode(';',$goods_le_arr);
	        
	        $delivery_time=date("H:i",$orderinfo['shipping_time']);
	        $openid='';
	        
	        $add_re=$this->add_comment($orderinfo, 5, "", $goods_level, $delivery_time, $openid);
	        if($add_re!=false){
	            echo $item['order_sn'].'<br>';
	        }
	    }
	    
	    echo 'Success';
	}
	
	private function add_comment($orderinfo,$level,$content,$goods_level,$delivery_time,$openid){
	    if(!empty($openid)){
	        $user_name = M("wechat_user")->where(array("openid"=>$openid))->getField("nickname");
	    }
	    if(empty($user_name)){
	        $user_name=substr_replace($orderinfo['mobile'],'****',3,4);
	    }
	    $data = array();
	    $data['user_id'] = $orderinfo['user_id'];
	    $data['status'] = 1;                        //状态，默认为不显示
	    $data['add_time'] = gmtime();
	    $data['comment_rank'] = $level;    //评价分数
	    $data['content'] = addslashes($content);   //评价内容
	    $data['user_name'] = $user_name;   //评价人名称
	    $data['distribution_time'] = $orderinfo['shipping_time']!=0?strtotime(date("Y-m-d",localtotime($orderinfo['shipping_time'])).' '.$delivery_time):0;   //配送时间
	    $data['sender_name'] = M("warehouse_personnel")->where(array("uid"=>$orderinfo['wp_userid']))->getField("name");   //配送时间
	    $data['order_sn'] = $orderinfo['order_sn'];   //关联订单号
	    $data['delivery_time'] = $delivery_time;   //送达时间
	
	    $add_re = M('comment')->add($data);
	    if($add_re!=false){
	        $goods_list=array();
	        foreach(explode(';',$goods_level) as $goods_item){
	            $thisGoods=explode(',',$goods_item);
	            $goods_list[$thisGoods[0]]=$thisGoods[1];
	        }
	        array_unique($goods_list);
	        foreach($goods_list as $key=>$value){
	            $goods = M("goods")->where(array("goods_id"=>$key))->find();
	            $data = array();
	            $data['comment_id'] = $add_re;
	            $data['goods_id'] = $key;
	            $data['goods_sn'] = $goods['goods_sn'];
	            $data['comment_rank'] = $value;    //评价分数
	            $data['goods_name'] = $goods['goods_name'];
	            M('comment_goods')->add($data);
	        }
	
	
	    }
	
	    return $add_re;
	
	}
	
	
	public function xlUrlAPI($url){
        /* 这是我申请的APPKEY，大家可以测试使用 */
        $key = '1562966081';
        $baseurl = 'http://api.t.sina.com.cn/short_url/shorten.json?source='.$key.'&url_long='.urlencode($url);
        $ch=curl_init();
        curl_setopt($ch, CURLOPT_URL,$baseurl);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        $strRes=curl_exec($ch);
        curl_close($ch);
        $arrResponse=json_decode($strRes,true);
        if (isset($arrResponse->error) || !isset($arrResponse[0]['url_long']) || $arrResponse[0]['url_long'] == '')
            return 0;
        return $arrResponse[0]['url_short'];
    }
	
}